$(document).ready(
    function() {
        getUserName();
        getCartInfo();
        refresh();
    }
);

//如果用户登录就显示用户名字，否则跳转到登录界面

function getUserName() {
    let userInfo = checkLoginName();
    if (userInfo == undefined || userInfo == "") {
        location.href = "login.html"
    }
    let html = template("userTpl", { userInfo });
    $("#userName").html(html);
}

function refresh() {
    //每次增加删除都改变高度
    let height = $(".y_Allgood").css("height");
    if (window.innerHeight > parseInt(height)) {
        $("section").css("height", window.innerHeight - 47);
    } else {
        $("section").css("height", height);
    }
}

function getCartInfo() {
    $.ajax({
        type: 'get',
        url: '/cart/queryCart',
        dataType: 'json',
        success: function(res) {
            let data = getCartItemCount(res);
            console.log(data);
            let html = template("cartTpl", { data });
            $("#good_Item_all").html(html);

        }
    });
}
//过滤重复元素
function getCartItemCount(data) {
    // js数组对象相同值相加，不同添加
    var newArr = [];
    data.forEach(item => {
        var dataItem = item;
        if (newArr.length > 0) {
            var filterValue = newArr.filter(v => {
                return v.productId == dataItem.productId
            })
            if (filterValue.length > 0) {
                newArr.forEach(n => {
                    if (n.productId == filterValue[0].productId) {
                        n.num = filterValue[0].num + dataItem.num;
                    }
                })
            } else {
                newArr.push(dataItem)
            }
        } else {
            newArr.push(dataItem)
        }
    });
    return newArr


}
//选择删除元素
function selectDelete() {

    var checkOnes = $("[name='checkOne']");
    let num = [];
    for (let i = 0; i < checkOnes.length; i++) {
        if (checkOnes[i].checked) {
            num[num.length] = parseInt(checkOnes[i].getAttribute("data-id"));
        }
    }
    console.log(num);
    if (num.length > 0)
        deleteCartInfo(num);
}
//删除购物车消息
function deleteCartInfo(mydata) {
    console.log(mydata);
    $.ajax({
        type: 'get',
        url: '/cart/deleteCart',
        data: {
            id: mydata
        },
        dataType: 'json',
        traditional: true,
        success: function(data) {
            location.reload();
            console.log("sucess");
        }
    });


}

//根据选中的ID来发送订单消息
//多个
function addOrderId() {

    var checkOnes = $("[name='checkOne']");
    let id = [];
    let num = [];
    for (let i = 0; i < checkOnes.length; i++) {
        if (checkOnes[i].checked) {
            id[id.length] = parseInt(checkOnes[i].getAttribute("data-id"));
            num[num.length] = parseInt($(".amount")[i].value);
        }
    }
    if (num.length > 0)
        addOrder(id, num);

}
//单个
function addOrder(id, n) {
    console.log(id, n);

    $.ajax({
        type: 'get',
        url: '/order/deleteOrder',
        dataType: 'json',
        success: function(data) {
            $.ajax({
                type: 'post',
                url: '/order/addOrder',
                data: {
                    productId: id,
                    num: n,
                },
                traditional: true,
                dataType: 'json',
                success: function(data) {
                    location.href = "order.html"
                }
            });

        }
    });
}